#include "show2D.h"

#include <QDebug>


Show2D::Show2D(QWidget * parent) : BaseWidget(parent)
{
    m_imgLabel = new NewQLabel();
	m_dataLabel = new QLabel();
	m_dataLabel->setStyleSheet("font: 18px; color: #ffffff");
    parent->layout()->addWidget(m_imgLabel);
	parent->layout()->addWidget(m_dataLabel);
    m_img = new QImage(640, 480, QImage::Format_RGB32);

	m_parent = parent;
}

Show2D::~Show2D()
{

}

void Show2D::__setData(std::shared_ptr<LvMat> & src, int min, int max)
{
    int i = src->row();
    int j = src->col();
    uint8_t r, g, b;
    for(int row = 0; row < src->row(); row++)
    {
        for(int col = 0; col < src->col(); col++)
        {
            if(src->dataLength == 2)
            {
                auto data = src->get<uint16_t>(row, col);
				int h = (data <= max && data >= min) ? data * 1.0 / (max - min) * 360 : 0;
				HSVtoRGB(&r, &g, &b, h);
				m_img->setPixel(col, row, (r << 16) | (g << 8) | b);
				// m_img->setPixel(col, row, 0xffffff);
                if(m_blackBGEnable && data == 0 || !(data <= max && data >= min))
				{
					m_img->setPixel(col, row, 0x000000);
				}
            }
            else if(src->dataLength == 1)
            {
                uint8_t data = src->get<uint8_t>(row, col);
                m_img->setPixel(col, row, (data << 16) | (data << 8) | data);
            }
        }
    }
	drawpos(NewQLabel::curPos.y(), NewQLabel::curPos.x());
	if(src->dataLength == 2)
	{
		QString text = QString().setNum(src->get<uint16_t>(NewQLabel::curPos.y(), NewQLabel::curPos.x())) + QString("mm");
		m_dataLabel->setText(text);
	}
	else
	{
		m_dataLabel->setNum(src->get<uint8_t>(NewQLabel::curPos.y(), NewQLabel::curPos.x()));
	}
	if(m_isSaveFlag)
	{
		if(src->dataLength == 2)
		{
			src->saveData<uint16_t>(m_filePath + "/Depth.csv");
			m_img->save(tr((m_filePath + "/Depth.png").c_str()));
		}
		else
		{
			src->saveData<uint8_t>(m_filePath + "/IR.csv");
			m_img->save(tr((m_filePath + "/IR.png").c_str()));
		}
		m_isSaveFlag = false;
	}

	m_imgLabel->setData(src);
}

void Show2D::__render()
{
	QPixmap pixmap;
	m_imgLabel->setScaledContents(false);
	m_imgLabel->setFullScreen(m_isFullScreen);
	if(m_isFullScreen)
	{
		pixmap = QPixmap::fromImage(m_img->scaled(640 * SCALARMAX, 480 * SCALARMAX));
	}
	else
	{
		m_imgScalar = slt(easyWebServer::Json)["ImgSizeScalar"].asDouble();
    	pixmap = QPixmap::fromImage(m_img->scaled(640 * m_imgScalar, 480 * m_imgScalar, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    	// pixmap = QPixmap::fromImage(m_img->scaled(640 / 2, 480 / 2));
		// pixmap.scaled(640, 480, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	}
    m_imgLabel->setPixmap(pixmap);
}

void Show2D::HSVtoRGB(uint8_t *r, uint8_t *g, uint8_t *b, uint16_t h, uint16_t s, uint16_t v)
{
	// R,G,B from 0-255, H from 0-360, S,V from 0-100
	int i;
	float RGB_min, RGB_max;
	RGB_max = v*2.55f;
	RGB_min = RGB_max*(100 - s) / 100.0f;

	i = h / 60;
	int difs = h % 60; // factorial part of h

					   // RGB adjustment amount by hue 
	float RGB_Adj = (RGB_max - RGB_min)*difs / 60.0f;

	switch (i) {
	case 0:
		*r = RGB_max;
		*g = RGB_min + RGB_Adj;
		*b = RGB_min;
		break;
	case 1:
		*r = RGB_max - RGB_Adj;
		*g = RGB_max;
		*b = RGB_min;
		break;
	case 2:
		*r = RGB_min;
		*g = RGB_max;
		*b = RGB_min + RGB_Adj;
		break;
	case 3:
		*r = RGB_min;
		*g = RGB_max - RGB_Adj;
		*b = RGB_max;
		break;
	case 4:
		*r = RGB_min + RGB_Adj;
		*g = RGB_min;
		*b = RGB_max;
		break;
	default:		// case 5:
		*r = RGB_max;
		*g = RGB_min;
		*b = RGB_max - RGB_Adj;
		break;
	}
}

void Show2D::setBlackBG(bool src)
{
	m_blackBGEnable = src;
}

void Show2D::drawpos(int row, int col)
{
	for(int i = row - CROSSSIZE, j = col - CROSSSIZE; i <= row + CROSSSIZE && j <= col + CROSSSIZE; i++, j++)
	{
		if(i > 0 && i < m_img->height())
		{
			QRgb color = m_img->pixel(col, i);
			m_img->setPixel(col, 
							i, 
							((255 - (color >> 16)) << 16) | 
							((255 - ((color & 0x0000ff00) >> 8)) << 8) | 
							((255 - (color & 0x000000ff))));
		}
		if(j > 0 && j < m_img->width())
		{
			QRgb color = m_img->pixel(j, row);
			m_img->setPixel(j, 
							row, 
							((255 - (color >> 16)) << 16) | 
							((255 - ((color & 0x0000ff00) >> 8)) << 8) | 
							((255 - (color & 0x000000ff))));
		}
	}	
}

void Show2D::__saveData(const std::string & filePath)
{
	m_filePath = filePath;
	m_isSaveFlag = true;
}